// vp_ju/pages/user/settings.js
var app = getApp();
var vp = require('../../resource/js/vp.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    editable: false,
    is_changed: false,

    avatar: '',
    nickname: '',
    mobile: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var self = this;

    app.util.getUserInfo(function (response) {
      self.setData({
        userInfo: response,

      });
      app.mdInit(function (cfg, mine, area) {
        self.setData({
          cfg: cfg,
          mine: mine,
          avatar: mine.avatar,
          main_loaded: true,
          nickname: mine.nickname
        });
      });
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getMobile: function (e) {
    let self = this;
    console.log(e.detail.code)
    wx.login({
      success: function (res) {
        console.log(res);
        e.detail.code = res.code
        app.util.getMobile(function (data) {
          console.log('getMobile', data.data.data.phoneNumber);
          console.log(data);
          self.setData({
            mobile: data.data.data.phoneNumber
          })
          self.profileSave();
        }, e.detail)
      },
      fail: function () {
        wx.showModal({
          title: '获取信息失败',
          content: '请允许授权以便为您提供给服务',
          success: function (res) {
            if (res.confirm) {

            }
          }
        })
      }
    });

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },


  // 修改禁止提示
  editDisabled: function (e) {
    wx.showToast({
      title: '还未到下次修改时间',
      icon: 'none',
      duration: 2000
    });
    return;
  },

  // 头像
  onAvatarChange: function (e) {
    var self = this;
    console.log(e)
    const {
      avatarUrl
    } = e.detail
    var url = app.util.url('Common/upload');
    wx.uploadFile({
      url: url,
      filePath: avatarUrl,
      name: 'file',
      formData: {
        'path': avatarUrl,
      },
      success: function (ret) {
        ret.data = JSON.parse(ret.data);
        console.log( ret.data.data.url);

        if (ret.data.code != 1) {
          wx.showModal({
            title: '图片上传失败',
            content: ret.data.message,
            showCancel: false,
            success(res) {
              self.setData({
                "avatar_file": '',
                "avatar": ''
              });
            }
          });
        } else {
          self.setData({
            "avatar": ret.data.data.url,
            "is_changed": true
          });
        }
      }
    });
  },

  onNicknameChange: function (e) {
    console.log(e)
    this.setData({
      is_changed: true,
      nickname: e.detail.value,
    });
  },

  profileSave: function (e) {
    var self = this;



    if (self.data.avatar_file && !self.data.avatar) {
      wx.showToast({
        title: '请稍等，头像图片正在上传',
        icon: 'none',
        duration: 2000
      });
      return;
    }
    // 昵称验证
    if (!self.data.nickname) {
      return wx.showToast({
        title: '请设置您的昵称',
        icon: 'none',
        duration: 2000
      });
    }

    app.util.request({
      url: 'User/profile',
      data: {
        m: app.mdName,
        submit: 'save',
        avatar: self.data.avatar,
        nickname: self.data.nickname,
        mobile: self.data.mobile
      },
      success(res) {
        app.updateMine(function (mine) {
          self.setData({
            mine: mine
          });
          wx.showToast({
            title: res.data.msg,
            icon: 'success',
            duration: 2000,
            success: function () {
              setTimeout(function () {
                wx.switchTab({
                  url: '/vp_pz/pages/user/index',
                })
              }, 1000);
            }
          });
        });
      }
    });
  },

})